﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SQLite;
using Com.Toook.Common;

public partial class UserControls_MovieRecommendControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitRecommend();
    }
    /// <summary>
    /// 随机推荐同类电影
    /// </summary>
    private void InitRecommend()
    {
        string type = Request["type"];        
        int p = 1;
        string typeCondition = "";
        //类型不为空且是数字，设置类型条件。否则不加类型条件。
        if(!string.IsNullOrEmpty(type) && int.TryParse(type, out p))
        {
            typeCondition = " where type=" + type;
        }
        else
        {
            type = "";
        }        

        DataRow row = PageData.SelectParamRow("typeCode='movie" + type + "'");
        int total = 1;
        if(row == null)//没有找到，类型不对.设置为默认
        {
            row = PageData.SelectParamRow("typeCode='movie'");           
        }
        int showCount = 15;//随机推荐显示数
        total = int.Parse(row[PageColoumn.totalCount].ToString());
        Random rand = new Random(DateTime.Now.Second);
        int start = rand.Next(total - showCount);
//MoviePublish

        string t = "";
        if(p == 31)
        {
            t = " where siteKindId = 'xddy_aq' ";
        }
        else if(p == 32)
        {
            t = " where siteKindId = 'xddy_xj' ";
        }
        else if(p == 33)
        {
            t = " where siteKindId = 'xddy_dh' ";
        }
        else if(p == 34)
        {
            t = " where siteKindId = 'xddy_gs' ";
        }
        else if(p == 35)
        {
            t = " where siteKindId = 'xddy_kb' ";
        }
        string sql = "select id, title from UncheckedMovieXiaoDiao " + t + // typeCondition +
            " limit " + start.ToString() + "," + showCount.ToString();
        SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(sql, conn);
        SQLiteDataReader reader = null;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            rptRecommend.DataSource = reader;
            rptRecommend.DataBind();
        }
        finally
        {
            SqliteHelper.CloseDbResource(conn, reader);
        }
    }
}
